package com.jrsoft.bim.util;

import com.jrsoft.bim.component.poi.ExcelParseUtil;
import com.jrsoft.bim.db.ConnDB;
import com.jrsoft.bim.db.DBV2Util;
import com.jrsoft.bim.dto.excel.ComponentDTO;
import com.jrsoft.bim.dto.excel.FieldDTO;
import com.jrsoft.bim.enums.ComponentCategoryEnum;
import com.jrsoft.bim.util.paser.WallParse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.*;

import static com.jrsoft.bim.util.MathUtil.ZERO;
import static com.jrsoft.bim.util.MathUtil.ZERO_STR;

/**
 * 根据word公式进行计算
 *
 * @author Blueeyedboy
 * @create 2019-12-20 5:34 PM
 **/
public class ConvertV2Util {


	public static DBV2Util dbUtil = null;

	public static void convert(String srcPath, String descFilePath){
		Map<ComponentCategoryEnum,List<ComponentDTO>> result = ExcelParseUtil.parse(srcPath);
		ClassPathResource resource = new ClassPathResource("evaluate-demo.ydb");
		//			File demo = resource.getFile();
		//			System.out.println(demo.getAbsolutePath()+"--------"+f.getAbsolutePath());

		File f = null;
		try {
			if(descFilePath.endsWith(".ydb")){
				f = new File(descFilePath);
			}else{
				f = new File(descFilePath + File.separator + DateUtil.getTimeMillis()+".ydb" );
			}
			if(f.exists()){
				f.deleteOnExit();
			}
			FileUtil.copyFile(resource.getInputStream(),new FileOutputStream(f));
		} catch (IOException e) {
			e.printStackTrace();
		}
		String dbPath = f.getAbsolutePath();
		ConnDB db = new ConnDB("jdbc:sqlite:"+dbPath,"","");
		dbUtil = new DBV2Util(db);
		String paiStr = "3.14";
		List<ComponentDTO> bridgeList = result.get(ComponentCategoryEnum.BRIDGE);
		if(bridgeList != null) {
			int index = 0;
			for (ComponentDTO c : bridgeList) {
				c.setId(++index);
				Map<String,FieldDTO> fields =c.getFieldDTOMap();
				/**
				* zz中的N读取KL50的支座数量-1 也就是跨数
				 * YLG中的N读取KL50的支座数量-1 也就是跨数
				*
				**/
				System.out.println("***********"+c.getCode()+"*********");
				/*if(c.getCode().equals("A21001-028-0203-Y01")){
					System.out.println("*****1111******"+c.getCode()+"*********");
				}*/
				// V(梁)=ZZ(3,i)* KL29* KL29+ KL31* KL32+ KL33* KL34
				String expression = "SUM + KL(28,1)*KL31*KL32 + KL(28,N)*KL33*KL34";
				FieldDTO kl50 = c.getFieldDTOMap().get("KL50");
				int n = getIntegerValue(kl50);
				Map<String, Object> params = new HashMap<>();
				Object value = null;
				/*params.put("SUM", getVSum(fields,n));
				params.put("KL(28,1)", getDoubleValue(fields.get("KL(28,1)")));
				params.put("KL31", getDoubleValue(fields.get("KL31")));
				params.put("KL32", getDoubleValue(fields.get("KL32")));
				params.put("KL(28,N)", Double.parseDouble(getNumString(fields,"KL",28,n)));
				params.put("KL33", getDoubleValue(fields.get("KL33")));
				params.put("KL34", getDoubleValue(fields.get("KL34")));*/

//				Object value = MathUtil.evaluate(expression, params);
//				System.out.println("--------o1-----"+value);
//				c.setV1(getDoubleValue(value));

				// (上部)：
				// 上排角筋: (2*(3.14/4)*YZS(1,1)²*(KL31+(KL19+12)*ZZS(1,1)+KL19*YZS(1,1)+ZZ(3,1)+{ZZ(2,[2,N-1,1])+ZZ(3,[2,N-1,1])}+KL19*YZS(1,N)+KL33+(KL19+12)*YZS(1,N))) *7.85
				expression = "( 2*(pai/4)*YZS(1,1)^2* ( ( KL31 + KL19*ZZS(1,1) )*KL30 + KL19*YZS(1,1)*2 + ZZ(3,[1,N,1]) + ZZ(1,[2,N-1,1])  ) ) *7.85/1000/1000";
				params.clear();
				expression = expression.replace("pai", paiStr);
				expression = expression.replace("YZS(1,1)", getNumStringValue(fields.get("YZS(1,1)")));
				expression = expression.replace("KL30", getNumStringValue(fields.get("KL30")));
				expression = expression.replace("KL31", getNumStringValue(fields.get("KL31")));
				expression = expression.replace("KL19", getNumStringValue(fields.get("KL19")));
				expression = expression.replace("ZZS(1,1)", getNumStringValue(fields.get("ZZS(1,1)")));
				expression = expression.replace("ZZ(1,1)", getNumStringValue(fields.get("ZZ(1,1)")));
				expression = expression.replace("ZZ(3,[1,N,1])", getNumStringSum(fields,"ZZ",3,1,n));
				expression = expression.replace("ZZ(1,[2,N-1,1])", getNumStringSum(fields,"ZZ",1,2,n-1));
				expression = expression.replace("YZS(1,N)", getNumString(fields,"YZS",1,n));
				expression = expression.replace("KL33", getNumStringValue(fields.get("KL33")));
				value = MathUtil.evaluate(expression, params);
				System.out.println("----上部上排角筋----o2-----"+value);
				c.setV2(getDoubleValue(value));

				// 上排中间筋： (YZS(2,1)*(3.14/4)*YZS(3,1)²*(KL31+(KL19+12)ZZS(1,1)+KL19*YZS(3,1)+ZZ(3,1)/3)+{YZS(2,[2,N-1,1])*(3.14/4)*YZS(3,[2,N-1,1])²*(ZZ(3,[1,N-2,1])/3+ZZ(3,[2,N-1,1])/3+ZZ(2,[2,N-1,1]))}+ZZS(2,N))*(3.14/4)*ZZS(3,N)²*(KL19*ZZS(3,N)+ZZ(3,N)/3+KL33+(KL19+12)*YZS(1,N))) *7.85
				expression = "7.85*(YZS(2,1)*pai/4*YZS(3,1)^2)*"
						+ "("
						+ "(KL31+KL19*ZZS(3,1))*KL30 + KL19*YZS(3,1)*2 +"
						+ "2/3*ZZ(3,[1,N,1]) + ZZ(1,[2,N-1,1])"
						+ ")/1000/1000 ";
				expression = expression.replace("pai", paiStr);
				expression = expression.replace("YZS(2,1)", getNumStringValue(fields.get("YZS(2,1)")));
				expression = expression.replace("YZS(3,1)", getNumStringValue(fields.get("YZS(3,1)")));
				expression = expression.replace("KL30", getNumStringValue(fields.get("KL30")));
				expression = expression.replace("KL31", getNumStringValue(fields.get("KL31")));
				expression = expression.replace("KL19", getNumStringValue(fields.get("KL19")));
				expression = expression.replace("ZZ(3,[1,N,1])", getNumStringSum(fields,"ZZ",3,1,n));
				expression = expression.replace("ZZ(1,[2,N-1,1])", getNumStringSum(fields,"ZZ",1,2,n-1));
				expression = expression.replace("ZZS(3,1)", getNumString(fields,"ZZS",3,1));
				value = MathUtil.evaluate(expression, params);
				System.out.println("----上部上排中间筋----o3-----"+value);
				c.setV3(getDoubleValue(value));
				// 上排贯通筋：
				expression = "YLT(1,1)*pai/4*YLT(2,1)^2*"
						+ "( KL31+KL33+2*KL19*YLT(2,1)+ZZ(3,1)"
						+ "  + (ZZ(2,[3,N-1,1]) + ZZ(3,[3,N-1,1])) + KL19*YLT(2,N) + (KL19+12)*YLT(2,N)"
						+ ")*7.85/1000/1000";
				expression = expression.replace("pai", paiStr);
				expression = expression.replace("YLT(1,1)", getNumStringValue(fields.get("YLT(1,1)")));
				expression = expression.replace("YLT(2,1)", getNumStringValue(fields.get("YLT(2,1)")));
				expression = expression.replace("KL31", getNumStringValue(fields.get("KL31")));
				expression = expression.replace("KL19", getNumStringValue(fields.get("KL19")));
				expression = expression.replace("ZZ(3,1)", getNumStringValue(fields.get("ZZ(3,1)")));
				expression = expression.replace("ZZ(2,[3,N-1,1])", getNumStringSum(fields,"ZZ",2,3,n-1));
				expression = expression.replace("ZZ(3,[3,N-1,1])", getNumStringSum(fields,"ZZ",3,3,n-1));
				expression = expression.replace("YLT(2,N)", getNumString(fields,"YLT",2,n));
				expression = expression.replace("KL33", getNumStringValue(fields.get("KL33")));
				value = MathUtil.evaluate(expression, params);
				System.out.println("-----上部上排贯通筋----o4-----"+value);
				c.setV4(getDoubleValue(value));

				// 下排： (YZS(4,1)*(3.14/4)*YZS(5,1)²*(0.75*KL31+(KL19+10)*YZS(3,1)+YZS(4,1)*(3.14/4)*YZS(5,1)²*(KL19*YZS(3,1)+ZZ(3,1)/4))+{(ZZ(3,[1,N-2,1])/4+ZZ(3,[2,N-1,1])/4+ZZ(2,[2,N-1,1]))*YZS(2,[2,N-1,1])*(3.14/4)*YZS(3,[2,N-1,1])²}+ZZS(2,N))*(3.14/4)*ZZS(3,N)²*((KL19*ZZS(3,N)+ZZ(3,N)/4)+(0.75*KL33+(KL19+10)*YZS(3,N)))*ZZS(4,N)*(3.14/4)*YZS(5,N)²*)*7.85
				expression = "("
						+ "YZS(4,1)*pai/4*YZS(5,1)^2*(0.75*KL31+(KL19+10)*YZS(3,1))"
						+ "+YZS(4,1)*pai/4*YZS(5,1)^2*(KL19*YZS(5,1) + ZZ(3,1)/4)"
						+ "+(ZZ(3,[3,N-2,1])/4 + ZZ(3,[3,N-1,1])/4 + ZZ(2,[3,N-1,1])) * YZS(2,[3,N-1,1]) * pai/4 * YZS^2(5,[3,N-1,1])"
						+ "+ZZS(4,N)* pai/4 * ZZS(5,N)^2 * (KL19*ZZS(5,N)+ZZ(3,N)/4)"
						+ "+ZZS(4,N)* pai/4 * YZS(5,N)^2 * (0.75*KL33+(KL19+10)*YZS(5,N))"
						+ ")*7.85/1000/1000";
				expression = "("
						+ "YZS(4,1)*pai/4*YZS(5,1)^2*"
						+ "("
						+ "KL31 + KL33 + 2*KL19*YZS(5,1)"
						+ "+ZZ(3,[1,N,1])/2 + ZZ(2,[2,N,1])"
						+ ")"
						+ ")*7.85/1000/1000";
				expression = expression.replace("pai", paiStr);
				expression = expression.replace("YZS(4,1)", getNumStringValue(fields.get("YZS(4,1)")));
				expression = expression.replace("YZS(5,1)", getNumStringValue(fields.get("YZS(5,1)")));
//				expression = expression.replace("YZS(3,1)", getNumStringValue(fields.get("YZS(3,1)")));
				expression = expression.replace("KL31", getNumStringValue(fields.get("KL31")));
				expression = expression.replace("KL19", getNumStringValue(fields.get("KL19")));
				/*expression = expression.replace("ZZ(3,1)", getNumStringValue(fields.get("ZZ(3,1)")));
				expression = expression.replace("ZZ(3,[3,N-2,1])", getNumStringSum(fields,"ZZ",3,3,n-2));
				expression = expression.replace("ZZ(3,[3,N-1,1])", getNumStringSum(fields,"ZZ",3,3,n-1));
				expression = expression.replace("ZZ(2,[3,N-1,1])", getNumStringSum(fields,"ZZ",2,3,n-1));
				expression = expression.replace("YZS(2,[3,N-1,1])", getNumStringSum(fields,"YZS",2,3,n-1));
				expression = expression.replace("YZS^2(5,[3,N-1,1])", getNumStringPowSum(fields,"YZS",5,3,n-1));
				expression = expression.replace("ZZ(3,N)", getNumString(fields,"ZZ",3,n));
				expression = expression.replace("ZZS(4,N)", getNumString(fields,"ZZS",4,n));
				expression = expression.replace("ZZS(5,N)", getNumString(fields,"ZZS",5,n));
				expression = expression.replace("YZS(5,N)", getNumString(fields,"YZS",5,n));*/
				expression = expression.replace("ZZ(3,[1,N,1])", getNumStringSum(fields,"ZZ",3,1,n));
				expression = expression.replace("ZZ(2,[2,N,1])", getNumStringSum(fields,"ZZ",2,2,n));
				expression = expression.replace("KL33", getNumStringValue(fields.get("KL33")));
				value = MathUtil.evaluate(expression, params);
				System.out.println("-----上部下排钢筋----o5-----"+value);
				c.setV5(getDoubleValue(value));

				// 箍筋(2)：（表格添加箍筋参数）((max(1.5*KL(29,[1,N-1,1]),500)/YLG(2,[1,N-1,1])*2+(ZZ(3,[1,N-1,1])-2*max(1.5*KL(29,[1,N-1,1]),500))/YLG(4,[1,N-1,1]))*3.14/4*YLG(1,[1,N-1,1])²*((KL(28,[1,N-1,1])+KL(29,[1,N-1,1]))*2-8*KL12+10*YLG(1,[1,N-1,1])+KL31/ ZLG(2,1)*3.14/4*ZLG(1,1)²*((KL(28,1)+KL(29,1))*2-8*KL12+10*YLG(1,1)+ KL33/ YLG(2,N) *3.14/4*YLG(1,N)²*((KL(28,N)+KL(29,N))*2-8*KL12+10*YLG(1,N))*7.85
				FieldDTO YLG41 = c.getFieldDTOMap().get("YLG(4,1)");
				double ylg41 = getDoubleValue(YLG41);
				if(ylg41>0){
					// 分段加密
					expression = "("
							+ "sub4"
							+ "+sub5"
							+ "+sub1"
							+ "+sub2"
							+ "+sub3"
							+ ")*7.85/1000/1000";

					params.clear();
					params.put("KL31",getNumStringValue(fields.get("KL31")));
					params.put("ZLG(2,1)",getNumString(fields,"ZLG",2,1));
					params.put("KL(28,1)",getNumString(fields,"KL",28,1));
					params.put("KL(29,1)",getNumString(fields,"KL",29,1));
					params.put("KL32",getNumStringValue(fields.get("KL32")));
					params.put("KL12",getNumStringValue(fields.get("KL12")));
					params.put("YLG(1,1)",getNumString(fields,"YLG",1,1));
					params.put("ZLG(1,1)",getNumString(fields,"ZLG",1,1));
					params.put("KL33",getNumStringValue(fields.get("KL33")));
					params.put("YLG(2,N)",getNumString(fields,"YLG",2,n));
					params.put("KL(28,N-1)",getNumString(fields,"KL",28,n-1));
					params.put("KL(29,N-1)",getNumString(fields,"KL",29,n-1));
					params.put("KL34",getNumStringValue(fields.get("KL34")));
					params.put("YLG(1,N-1)",getNumString(fields,"YLG",1,n-1));
					params.put("pai", paiStr);
					double subSum1 = 0;
					double subSum2 = 0;
					double subSum3 = 0;
					double subSum4 = 0;
					double subSum5 = 0;

					for(int i = 1;i<=n;i++){
						String subExpression1 = "( max(1.5*KL29_i,500)/YLG(2,i)*2 + ( ZZ(3,i) - max(1.5*KL29_i,500)*2 )/YLG(4,i) )"
								+ "* ( 2*KL(28,i) + 2*KL(29,i) - 8*KL12 + 2*13.57*YLG(1,i) )*pai/4*YLG(1,i)^2";
						String subExpression2 = "JY(1,i)/YLG(2,i)*JY(2,i)*pai/4*YLG(1,i)^2";
						String subExpression3 = "JY(3,i)/YLG(2,i)*JY(4,i)*pai/4*YLG(1,i)^2";

						double maxKl29 = MathUtil.getMax(500,1.5*getNumSum(fields,"KL",29,i,i));
						Map<String,Object> subParams = new HashMap<>();
						subParams.put("max(1.5*KL29_i,500)", String.valueOf(maxKl29));
						subParams.put("KL(29,i)", getNumString(fields,"KL",29,i));
						subParams.put("YLG(2,i)", getNumString(fields,"YLG",2,i));
						subParams.put("ZZ(3,i)", getNumString(fields,"ZZ",3,i));
						subParams.put("YLG(4,i)", getNumString(fields,"YLG",4,i));
						subParams.put("KL(28,i)", getNumString(fields,"KL",28,i));
						subParams.put("KL12", getNumStringValue(fields.get("KL12")));
						subParams.put("YLG(1,i)", getNumString(fields,"YLG",1,i));
						subParams.put("JY(1,i)", getNumString(fields,"JY",1,i));
						subParams.put("YLG(2,i)", getNumString(fields,"YLG",2,i));
						subParams.put("JY(2,i)", getNumString(fields,"JY",2,i));
						subParams.put("JY(3,i)", getNumString(fields,"JY",3,i));
						subParams.put("JY(4,i)", getNumString(fields,"JY",4,i));
						subParams.put("pai", paiStr);

						double sub1 = MathUtil.evaluateDouble(subExpression1,subParams);
						double sub2 = MathUtil.evaluateDouble(subExpression2,subParams);
						double sub3 = MathUtil.evaluateDouble(subExpression3,subParams);
						subSum1 = subSum1 + sub1;
						subSum2 = subSum2 + sub2;
						subSum3 = subSum3 + sub3;
					}

					String subExpression4 = "KL31/ZLG(2,1)* ( 2*KL(28,1) + ( KL(29,1)+KL32 ) -8*KL12 + 2*13.57*ZLG(1,1) ) *pai/4*ZLG(1,1)^2";
					String subExpression5 = "KL33/YLG(2,N)* ( 2*KL(28,N-1) + ( KL(29,N-1) +KL34 )-8*KL12 + 2*13.57*YLG(1,N-1) ) *pai/4*YLG(1,N-1)^2";

					subSum4 = MathUtil.evaluateDouble(subExpression4,params);
					subSum5 = MathUtil.evaluateDouble(subExpression5,params);

					params.put("sub1",subSum1);
					params.put("sub2",subSum2);
					params.put("sub3",subSum3);
					params.put("sub4",subSum4);
					params.put("sub5",subSum5);
				}else{

					// 全段加密
					expression = "("
							+"KL31/ZLG(2,1)* ( 2*KL(28,1) + ( KL(29,1)+KL32 ) -8*KL12 + 2*13.57*ZLG(1,1) ) *pai/4*ZLG(1,1)^2"
							+"+KL33/YLG(2,N)* ( 2*KL(28,N-1) + ( KL(29,N-1) +KL34 )-8*KL12 + 2*13.57*YLG(1,N-1) ) *pai/4*YLG(1,N-1)^2"
							+ "+( ( KL25-KL21+KL26-(KL22) )/YLG(2,1) ) "
							+ "* ( 2*KL(28,1) + 2*KL(29,1) - 8*KL12 + 2*13.57*YLG(1,1) )*pai/4*YLG(1,1)^2"
							+ "+sub2"
							+ "+sub3"
							+ ")*7.85/1000/1000";

					params.clear();
					params.put("KL31",getNumStringValue(fields.get("KL31")));
					params.put("ZLG(2,1)",getNumStringNotZero(fields,"ZLG",2,1));
					params.put("KL(28,1)",getNumString(fields,"KL",28,1));
					params.put("KL(29,1)",getNumString(fields,"KL",29,1));
					params.put("KL32",getNumStringValue(fields.get("KL32")));
					params.put("KL12",getNumStringValue(fields.get("KL12")));
					params.put("YLG(1,1)",getNumString(fields,"YLG",1,1));
					params.put("ZLG(1,1)",getNumString(fields,"ZLG",1,1));
					params.put("KL33",getNumStringValue(fields.get("KL33")));
					params.put("YLG(2,N)",getNumStringNotZero(fields,"YLG",2,n));
					params.put("KL(28,N-1)",getNumString(fields,"KL",28,n-1));
					params.put("KL(29,N-1)",getNumString(fields,"KL",29,n-1));
					params.put("KL34",getNumStringValue(fields.get("KL34")));
					params.put("YLG(1,N-1)",getNumString(fields,"YLG",1,n-1));
					params.put("pai", paiStr);
					params.put("KL25", getNumStringValue(fields.get("KL25")));
					params.put("KL21", getNumStringValue(fields.get("KL21")));
					params.put("KL26", getNumStringValue(fields.get("KL26")));
					params.put("KL22", getNumStringValue(fields.get("KL22")));
					params.put("YLG(2,1)", getNumString(fields,"YLG",2,1));
					params.put("KL(29,1)", getNumString(fields,"KL",29,1));
					params.put("KL(28,1)", getNumString(fields,"KL",28,1));
					double subSum1 = 0;
					double subSum2 = 0;
					double subSum3 = 0;

					for(int i = 1;i<=n;i++){
						String subExpression2 = "JY(1,i)/YLG(2,i)*JY(2,i)*pai/4*YLG(1,i)^2";
						String subExpression3 = "JY(3,i)/YLG(2,i)*JY(4,i)*pai/4*YLG(1,i)^2";

						Map<String,Object> subParams = new HashMap<>();
						subParams.put("KL25", getNumStringValue(fields.get("KL25")));
						subParams.put("KL21", getNumStringValue(fields.get("KL21")));
						subParams.put("KL26", getNumStringValue(fields.get("KL26")));
						subParams.put("KL22", getNumStringValue(fields.get("KL22")));
						subParams.put("YLG(2,i)", getNumString(fields,"YLG",2,i));
						subParams.put("KL(29,i)", getNumString(fields,"KL",29,i));
						subParams.put("KL(28,i)", getNumString(fields,"KL",28,i));
						subParams.put("KL12", getNumStringValue(fields.get("KL12")));
						subParams.put("YLG(1,i)", getNumString(fields,"YLG",1,i));
						subParams.put("JY(1,i)", getNumString(fields,"JY",1,i));
						subParams.put("YLG(2,i)", getNumString(fields,"YLG",2,i));
						subParams.put("JY(2,i)", getNumString(fields,"JY",2,i));
						subParams.put("JY(3,i)", getNumString(fields,"JY",3,i));
						subParams.put("JY(4,i)", getNumString(fields,"JY",4,i));
						subParams.put("pai", paiStr);

//						double sub1 = MathUtil.evaluateDouble(subExpression1,subParams);
						double sub2 = MathUtil.evaluateDouble(subExpression2,subParams);
						double sub3 = MathUtil.evaluateDouble(subExpression3,subParams);
						if(i == 1){
							subSum1 = subSum1 + 0;
						}

						subSum2 = subSum2 + sub2;
						subSum3 = subSum3 + sub3;
					}

					params.put("sub1",subSum1);
					params.put("sub2",subSum2);
					params.put("sub3",subSum3);
				}

				value = MathUtil.evaluate(expression, params);
				System.out.println("----上部箍筋----o6-----"+value);
				c.setV6(getDoubleValue(value));

				// 附加箍筋：YZF(1,[1,N-1,1])*3.14/4*YZF(2,[1,N-1,1])²*((KL(28,[1,N-1,1])+KL(29,[1,N-1,1]))*2-8*KL12+10*YZF (1,[1,N-1,1]))*7.85
				expression = "subSum1*7.85/1000/1000";
				double subSum1 = 0;
				for(int i = 1;i<=n;i++){
					String subExpression1 = "YZF(1,i)*pai/4*YZF(2,i)^2 "
							+ "* ( 2*KL(28,i) + 2*KL(29,i) - 8*KL12 + 2*13.57*YZF(2,i) )";

					Map<String,Object> subParams = new HashMap<>();
					subParams.put("YZF(1,i)", getNumString(fields,"YZF",1,i));
					subParams.put("YZF(2,i)", getNumString(fields,"YZF",2,i));
					subParams.put("KL(29,i)", getNumString(fields,"KL",29,i));
					subParams.put("KL(28,i)", getNumString(fields,"KL",28,i));
					subParams.put("KL12", getNumStringValue(fields.get("KL12")));
					subParams.put("pai", paiStr);

					double sub1 = MathUtil.evaluateDouble(subExpression1,subParams);
					subSum1 = subSum1 + sub1;
				}
				params.put("subSum1",subSum1);
				value = MathUtil.evaluate(expression, params);
				System.out.println("----上部附加箍筋----o7-----"+value);
				c.setV7(getDoubleValue(value));

				// 架力筋：(ZZ(3,[1,N-1,1])/3+300)* ZZF(1, N-1)*3.14/4 ZZF(2, N-1)²*7.85
				expression = "(ZZ(3,[1,N-1,1])/3+300)* ZZF(1,N-1)*3.14/4*ZZF(2,N-1)^2*7.85";
				expression = expression.replace("ZZ(3,[1,N-1,1])", getNumStringSum(fields,"ZZ",3,1,n-1));
				expression = expression.replace("ZZF(1,N-1)", getNumString(fields,"ZZF",1,n-1));
				expression = expression.replace("ZZF(2,N-1)", getNumString(fields,"ZZF",2,n-1));
				value = MathUtil.evaluate(expression, params);
				System.out.println("--------o8-----"+value);
				c.setV8(getDoubleValue(value));

				// 构造钢筋
				expression = "subSum1*7.85/1000/1000";
				subSum1 = 0;
				for(int i = 1;i<=n;i++){
					String subExpression1 = "(ZZ(3,i) + 2*15*YZG(2,i))"
							+ "* YZG(1,i) *pai/4* YZG(2,i)^2";

					Map<String,Object> subParams = new HashMap<>();
					subParams.put("YZG(1,i)", getNumString(fields,"YZG",1,i));
					subParams.put("YZG(2,i)", getNumString(fields,"YZG",2,i));
					subParams.put("ZZ(3,i)", getNumString(fields,"ZZ",3,i));
					subParams.put("pai", paiStr);

					double sub1 = MathUtil.evaluateDouble(subExpression1,subParams);
					subSum1 = subSum1 + sub1;
				}
				params.put("subSum1",subSum1);
				value = MathUtil.evaluate(expression, params);
				System.out.println("----构造钢筋----o71-----"+value);
				c.setV71(getDoubleValue(value));


				// (下部):
				// 上排贯通筋(下部上排)：YZX(3,1)*(3.14/4)*YZX(4,1)²*(KL31+KL19*YZX (2,1)+ZZ(3,1)+{2*max(0.5*ZZ(2,[2,N-1,1]),5*YZX(4,1))+ZZ(3,[2,N-1,1])}+KL19*YZX(4,N)+KL33)*7.85
				expression = "( YZX(3,1)*pai/4*YZX(4,1)^2 * "
						+ "( KL31 + 2*KL19*YZX(4,1) + KL33 +KL25 -KL21 + KL26 -KL22 )"
						+ ")*7.85/1000/1000";
				params.clear();
				params.put("YZX(3,1)",getNumString(fields,"YZX",3,1));
				params.put("YZX(4,1)",getNumString(fields,"YZX",4,1));
				params.put("KL31",getNumStringValue(fields.get("KL31")));
				params.put("KL19",getNumStringValue(fields.get("KL19")));
				params.put("KL33",getNumStringValue(fields.get("KL33")));
				params.put("KL25",getNumStringValue(fields.get("KL25")));
				params.put("KL21",getNumStringValue(fields.get("KL21")));
				params.put("KL26",getNumStringValue(fields.get("KL26")));
				params.put("KL22",getNumStringValue(fields.get("KL22")));
				params.put("pai", paiStr);

				/*subSum1 = 0;
				for(int i = 1;i<=n;i++){
					// 2* max(0.5*zz(2,i),5*YZX(4,1)) + zz(3,i)
					String subExpression1 = "2*max + zz(3,i) ";
					double max= MathUtil.getMax(0.5 * getNumSum(fields,"zz",2,i,i),5 * getNumSum(fields,"YZX",4,1,1));
					Map<String,Object> subParams = new HashMap<>();
					subParams.put("max", max);
					subParams.put("zz(3,i)", getNumString(fields,"ZZ",3,i));

					double sub1 = MathUtil.evaluateDouble(subExpression1,subParams);
					subSum1 = subSum1 + sub1;
				}
				params.put("subSum1",subSum1);*/
				value = MathUtil.evaluate(expression, params);
				System.out.println("----下部上排----o9-----"+value);
				c.setV9(getDoubleValue(value));

				// 下排贯通筋(下排角筋)：YZX(1,1)*(3.14/4)*YZX(2,1)^2*(KL31+KL19*YZX (2,1)+ZZ(3,1)+{2*max(0.5*ZZ(2,[2,N-1,1]),5*YZX(2,1))+ZZ(3,[2,N-1,1])}+KL19*YZX(2,N)+KL33) *7.85
				expression = "( YZX(1,1)*pai/4*YZX(2,1)^2 * "
						+ "( KL31 + KL33 + KL19*YZX(2,1)*2 + KL25 - KL21 + KL26 - KL22 )"
						+ ")*7.85/1000/1000";
				params.clear();
				params.put("YZX(1,1)",getNumString(fields,"YZX",1,1));
				params.put("YZX(2,1)",getNumString(fields,"YZX",2,1));
				params.put("KL33",getNumStringValue(fields.get("KL33")));
				params.put("KL31",getNumStringValue(fields.get("KL31")));
				params.put("KL19",getNumStringValue(fields.get("KL19")));
				params.put("KL25",getNumStringValue(fields.get("KL25")));
				params.put("KL21",getNumStringValue(fields.get("KL21")));
				params.put("KL26",getNumStringValue(fields.get("KL26")));
				params.put("KL22",getNumStringValue(fields.get("KL22")));
				params.put("pai", paiStr);
/*
				subSum1 = 0;
				for(int i = 1;i<=n;i++){
					// 2* max(0.5*zz(2,i),5*YZX(2,1)) + zz(3,i)
					String subExpression1 = "2*max + zz(3,i) ";
					double max= MathUtil.getMax(0.5 * getNumSum(fields,"zz",2,i,i),5 * getNumSum(fields,"YZX",2,i,i));
					Map<String,Object> subParams = new HashMap<>();
					subParams.put("max", max);
					subParams.put("zz(3,i)", getNumString(fields,"ZZ",3,i));

					double sub1 = MathUtil.evaluateDouble(subExpression1,subParams);
					subSum1 = subSum1 + sub1;
				}
				params.put("subSum1",subSum1);*/
				value = MathUtil.evaluate(expression, params);
				System.out.println("----下部下排角筋----o10-----"+value);
				c.setV10(getDoubleValue(value));

				// 下部下排中间筋：
				expression = "( YZX(5,1)*pai/4*YZX(6,1)^2 * "
						+ "( ( KL31+2*KL19*YZX(6,1) ) + KL33 + KL25 - KL21 + KL26 -KL22)"
						+ ")*7.85/1000/1000";
				params.clear();
				params.put("YZX(5,1)",getNumString(fields,"YZX",5,1));
				params.put("YZX(6,1)",getNumString(fields,"YZX",6,1));
				params.put("KL33",getNumStringValue(fields.get("KL33")));
				params.put("KL31",getNumStringValue(fields.get("KL31")));
				params.put("KL19",getNumStringValue(fields.get("KL19")));
				params.put("KL25",getNumStringValue(fields.get("KL25")));
				params.put("KL21",getNumStringValue(fields.get("KL21")));
				params.put("KL26",getNumStringValue(fields.get("KL26")));
				params.put("KL22",getNumStringValue(fields.get("KL22")));
				params.put("pai", paiStr);
				value = MathUtil.evaluate(expression, params);
				System.out.println("----下部下排中间筋----o11-----"+value);
				c.setV11(getDoubleValue(value));

				// 抗扭钢筋：
				expression = "(YZN(2,1)^2*YZN(1,1)* ( ZZ(3,1) + 2*KL19*YZN(2,1) ) )*pai/4*7.85/1000/1000";
				params.clear();
				params.put("YZN(2,1)",getNumString(fields,"YZN",2,1));
				params.put("YZN(1,1)",getNumString(fields,"YZN",1,1));
				params.put("ZZ(3,1)",getNumString(fields,"ZZ",3,1));
				params.put("KL19",getNumStringValue(fields.get("KL19")));
				params.put("pai", paiStr);
				value = MathUtil.evaluate(expression, params);
				System.out.println("----抗扭钢筋----o12-----"+value);
				c.setV12(getDoubleValue(value));

				// 先插入梁组件
				c.setCategoryEnum(ComponentCategoryEnum.BRIDGE);
				dbUtil.insertBridge(c);
			}
		}
		// 处理墙
		List<ComponentDTO> wallList = result.get(ComponentCategoryEnum.WALL);

		new WallParse().parse(wallList,dbUtil);

		System.out.println("--------开始执行数据库--------");
		db.beginTransaction();
		String[] sqlArr = db.getSql().split(";");
		for(String s : sqlArr){
			int i = db.executeUpdateNotClose(s);
			if(i==0){
				break;
			}

		}
		db.commitTransaction();
		db.close();
		System.out.println("--------结束执行数据库--------");


		System.out.println("--------拷贝结果文件--------");
		try {
			if(descFilePath.indexOf("梁")>-1){

				IOUtils.copy(new FileInputStream("E:\\工作\\项目\\bim\\template\\决赛工程量汇总表-梁.xlsx"),new FileOutputStream(descFilePath+File.separator+"决赛工程量汇总表-梁.xlsx"));

			}else if(descFilePath.indexOf("墙")>-1){
				IOUtils.copy(new FileInputStream("E:\\工作\\项目\\bim\\template\\决赛工程量汇总表-墙柱.xlsx"),new FileOutputStream(descFilePath+File.separator+"决赛工程量汇总表-墙柱.xlsx"));

			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	* 计算(V)梁中求和公式
	* @param 
	* @return 
	* @author Blueeyedboy
	* @create 4/30/2020 5:59 PM
	**/
	public static double getVSum(Map<String,FieldDTO> fields,int end){
		return MathUtil.getVSum(fields,end);
	}

	public static String getNumStringSum(Map<String,FieldDTO> fields,String prefix,int start,int begin,int end){

		return MathUtil.getNumStringSum(fields,prefix,start,begin,end);
	}

	public static String getNumStringPowSum(Map<String,FieldDTO> fields,String prefix,int start,int begin,int end){
		return MathUtil.getNumStringPowSum(fields,prefix,start,begin,end);
	}

	public static double getNumSum(Map<String,FieldDTO> fields,String prefix,int start,int begin,int end){

		return MathUtil.getNumSum(fields, prefix, start, begin, end);
	}

	public static String getNumStringNotZero(Map<String,FieldDTO> fields,String prefix,int start,int end){
		return MathUtil.getNumStringNotZero(fields, prefix, start, end);
	}

	public static String getNumString(Map<String,FieldDTO> fields,String prefix,int start,int end){
		return MathUtil.getNumString(fields, prefix, start, end);
	}

	public static double getDoubleValue(FieldDTO field){
		return MathUtil.getDoubleValue(field);
	}

	public static double getDoubleValue(Object o){
		return MathUtil.getDoubleValue(o);
	}

	public static int getIntegerValue(FieldDTO field){
		return MathUtil.getIntegerValue(field);
	}

	public static String getNumStringValue(FieldDTO field){
		return MathUtil.getNumStringValue(field);
	}

	public static void main(String[] args) {

		String srcFilePath = "E:\\工作\\项目\\bim\\2020-05-27";
		srcFilePath = "E:\\工作\\项目\\bim\\demo";
		String descFilePath = srcFilePath+File.separator+"result.ydb";
		convert(srcFilePath,descFilePath);

		/*ConnDB db = new ConnDB("jdbc:sqlite::resource:demo-yjk.ydb","","");
		Vector<Map<String,Object>> data = db.getData(" select name from sqlite_master where type = 'table'");
		Optional.ofNullable(data).ifPresent(t->t.forEach(item->ConvertUtil.clearTable(db,item)));*/

	}

	public static int getLevelByCode(String code){
		code = code.trim();
		code = code.substring(code.indexOf("-")+1);
		code = code.substring(code.indexOf("-")+1,code.lastIndexOf("-"));
		return Integer.parseInt(code);
	}

	private static void clearTable(ConnDB db,Map<String,Object> item){
		String tableName = String.valueOf(item.get("name"));
		System.out.println("delete from "+tableName+";");
		db.executeUpdateSql("delete from "+tableName);
	}
}
